package com.deyang.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deyang.workflow.entity.WorkOrderBase;
import com.deyang.workflow.entity.WorkOrderStep;
import com.deyang.workflow.entity.WorkOrderTemplate;
import com.deyang.workflow.entity.WorkOrderTemplateLog;
import com.deyang.workflow.mapper.WorkOrderStepMapper;
import com.deyang.workflow.mapper.WorkOrderTemplateMapper;
import com.deyang.workflow.service.IWorkOrderStepService;
import com.deyang.workflow.service.IWorkOrderTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deyang.workflow.vo.TemplateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 模板表 服务实现类
 * </p>
 *
 * @author zhanghuaiyu
 * @since 2021-07-01
 */
@Service
@Transactional
@Slf4j
public class WorkOrderTemplateServiceImpl extends ServiceImpl<WorkOrderTemplateMapper, WorkOrderTemplate> implements IWorkOrderTemplateService {

    @Autowired
    private WorkOrderTemplateMapper workOrderTemplateMapper;

    @Autowired
    private WorkOrderStepMapper workOrderStepMapper;

    @Autowired
    private IWorkOrderStepService workOrderStepService;


    @Override
    public void saveTemplate(TemplateVO templateVO) {
        log.info("---------------进入模板新增接口"+templateVO.toString());
        //新增模板数据到对应的表中
        if (null != templateVO) {
            try {
                //1.新增模板基础信息到模板表
                if (null != templateVO.getWorkOrderTemplate()) {
                    //手动新增创建时间
                    templateVO.getWorkOrderTemplate().setCreateTime(new Date());
                    workOrderTemplateMapper.insert(templateVO.getWorkOrderTemplate());
                    log.info("-------------------模板基本信息新增成功");
                }

                //2.新增模板步骤信息到模板步骤表
                if (null != templateVO.getWorkOrderStepList() && templateVO.getWorkOrderStepList().size() > 0) {
                    workOrderStepService.saveBatch(templateVO.getWorkOrderStepList());
                    log.info("--------------------模板步骤新增成功");
                }
            } catch (Exception e) {
                log.error("模板新增报错" + e.getMessage());
            }
        }
    }

    @Override
    public IPage<WorkOrderTemplate> queryTemplate(WorkOrderTemplate workOrderTemplate) {

        //分页条数插入
        //根据页数查询模板记录，不传值默认查询所有
        Page<WorkOrderBase> page = new Page<>(workOrderTemplate.getPageIndex(), workOrderTemplate.getPageSize());
        IPage<WorkOrderTemplate> workOrderTemplateIPage = workOrderTemplateMapper.queryTemplate(page, workOrderTemplate);
        return workOrderTemplateIPage;
    }

    @Override
    public TemplateVO echoTemplate(String templateCode) {
        log.info("------------------------------模板id为" + templateCode);
        try {
            WorkOrderTemplate workOrderTemplate = workOrderTemplateMapper.selectById(templateCode);
            QueryWrapper<WorkOrderStep> queryWrapper = new QueryWrapper<>();
            //构建条件查询模板步骤，查询模板编码和状态为1的数据
            queryWrapper.eq("template_code", templateCode).eq("status", "1");
            List<WorkOrderStep> workOrderSteps = workOrderStepMapper.selectList(queryWrapper);
            TemplateVO templateVO = new TemplateVO();
            templateVO.setWorkOrderTemplate(workOrderTemplate);
            templateVO.setWorkOrderStepList(workOrderSteps);
            return templateVO;
        } catch (Exception e) {
            log.error("------------------------模板回显报错" + e.getMessage());
        }

        return null;
    }

    @Override
    public void editTemplate(WorkOrderTemplate workOrderTemplate) {
        workOrderTemplateMapper.updateById(workOrderTemplate);
    }
}
